// pages/detailed-location-page/detailed-location-page.js
Page({
    data: {
        showLoadingState:false,
    // 场馆信息
        //图片
        p1:"https://pic.imgdb.cn/item/62da5797f54cd3f937994fd1.png",
        p4:"https://pic.imgdb.cn/item/62f9b06216f2c2beb183b2cc.png",
        p2:"https://pic.imgdb.cn/item/62da5799f54cd3f9379959d7.png",
        p14:"https://pic.imgdb.cn/item/62f4598b16f2c2beb185c3e8.png",
        p3:"https://pic.imgdb.cn/item/62f9b06216f2c2beb183b2be.png",
        p5:"https://pic.imgdb.cn/item/62da57a4f54cd3f937999196.png",
        p6:"https://pic.imgdb.cn/item/62da57a8f54cd3f93799a553.png",
        p16:"https://pic.imgdb.cn/item/62dfd88bf54cd3f937934a0d.png",
        //内容
        locationId:0,
        locationName:"",
        location:{
            location_id:123,
            location_name:'大学生活动中心',
            location:'华东师范大学。。。',
            location_type_room:0,
            location_type_free:0,
            location_open_time:9,
            location_close_time:10,
            location_sports:'羽毛球 乒乓球 篮球',
            location_state:0,
        },
        //转换数组
        location_type_room_array:['室外','室内'],
        location_type_free_array:['付费','免费'],
        location_state_array:['下架','上架'],
        //picker数据
        sportsArray:['所有项目','足球','篮球','排球','羽毛球','乒乓球','棒球','网球'],
        type:[['室外','室内'],['付费','免费']],
        type_index:[0,0],
        time_array:[['8:00','9:00','10:00','11:00','12:00','13:00','14:00','15:00','16:00','17:00','18:00','19:00','20:00','21:00','22:00'],['8:00','9:00','10:00','11:00','12:00','13:00','14:00','15:00','16:00','17:00','18:00','19:00','20:00','21:00','22:00','23:00']],
        time_array_:['8:00','9:00','10:00','11:00','12:00','13:00','14:00','15:00','16:00','17:00','18:00','19:00','20:00','21:00','22:00','23:00'],
        time_index:[0,0],//第二个数字代表偏移量-1，如果前面是6点，那么后面就是6+1+i；

    //场地信息
        //图片
        p7:"https://pic.imgdb.cn/item/62da57b8f54cd3f9379a01bf.png",
        p8:"https://pic.imgdb.cn/item/62da57acf54cd3f93799bed6.png",
        p9:"https://pic.imgdb.cn/item/62da57b0f54cd3f93799d518.png",
        p10:"https://pic.imgdb.cn/item/62da57b4f54cd3f93799ec93.png",
        p11:"https://pic.imgdb.cn/item/62da57bcf54cd3f9379a1647.png",
        p15:"https://pic.imgdb.cn/item/62f9b1ac16f2c2beb186b283.png",
        p13:"https://pic.imgdb.cn/item/62dab633f54cd3f937b439ed.png",
        //数据转换
        sportsArray:['所有项目','足球','篮球','排球','羽毛球','乒乓球','棒球','网球'],
        //stadium数据
        stadiums:[
            {
                stadium_id:123,
                stadium_name:"GroundA",
                stadium_sports:1,
                stadium_price:10,
                stadium_state:0,
            },
        ],

    },
    onLoad(options) {
        var that = this;
        var id = options.locationId;
        var name = options.locationName;
        that.setData({
            "locationId":id,
            "locationName":name,
        })
        wx.request({
          url: getApp().globalData.serverUrl,
          method:"GET",
          timeout:10000,
          data:{
            //各种数据的传送
            operation:"DETAILED_LOCATION",
            locationId:id,
            locationName:name,
          },
          success(res){
            if(res.data == false){
                wx.showModal({
                    showCancel:false,
                    title:"请求失败,请重试",
                });
            }
            else{
                //成功获取到信息后的操作
                console.log(res);
                that.setData({
                    "location":res.data.location,
                    "stadiums":res.data.stadiums.pdl,
                })
            }
          },
          fail(res){
            wx.showModal({
                showCancel:false,
                title:"请求超时,请重试",
            });
          },
        });
    },
    onShow(options) {
        var that = this;
        var id = that.data.locationId;
        var name = that.data.locationName;
        wx.request({
          url: getApp().globalData.serverUrl,
          method:"GET",
          timeout:10000,
          data:{
            //各种数据的传送
            operation:"DETAILED_LOCATION",
            locationId:id,
            locationName:name,
          },
          success(res){
            if(res.data == false){
                wx.showModal({
                    showCancel:false,
                    title:"请求失败,请重试",
                });
            }
            else{
                //成功获取到信息后的操作
                console.log({
                    //各种数据的传送
                    operation:"DETAILED_LOCATION",
                    locationId:id,
                    locationName:name,
                  },);
                that.setData({
                    "location":res.data.location,
                    "stadiums":res.data.stadiums.pdl,
                })
            }
          },
          fail(res){
            wx.showModal({
                showCancel:false,
                title:"请求超时,请重试",
            });
          },
        });
    },
    onChangeLocationName:function(e){
        var that = this;
        wx.showModal({
          title:"请输入新名字",
          editable:true,
          success(res){
              if(res.confirm){
                that.setData({
                    [`location.location_name`]:res.content,
                })
              };
          }
        })
    },
    onChangeLocation:function(e){
        var that = this;
        wx.showModal({
          title:"请输入新地址",
          editable:true,
          success(res){
              if(res.confirm){
                that.setData({
                    [`location.location`]:res.content,
                })
              };
          }
        })
    },
    onChangeProperty:function(e){
        var x = e.detail.value;
        this.setData({
            [`location.location_type_room`]:x[0],
            [`location.location_type_free`]:x[1],
        })
    },
    onChangeSports:function(e){
        this.setData({
            [`location.location_sports`]:e.detail.value,
        })
    },
    onChangeTime:function(e){
        var x = e.detail.value;
        this.setData({
            [`location.location_open_time`]:x[0]+8,
            [`location.location_close_time`]:x[0]+x[1]+9,
            'time_index':e.detail.value,
        })
        //初始化数组
        var array = this.data.time_array_.slice();
        this.setData({
            'time_array[1]':array.splice(0,1),
        })
    },
    onBindTime:function(e){
        var array = this.data.time_array_.slice();
        array.splice(0,this.data.time_index[0]+1);
        this.setData({
            'time_array[1]':array,
        })
    },
    onColumnChange:function(e){
        var column = e.detail.column;
        if(column == 0){
            var array = this.data.time_array_.slice();
            array.splice(0,e.detail.value + 1);
            this.setData({
                'time_array[1]':array,
            })
        }
    },
    onChangeLocationState:function(e){
        this.data.location.location_state = (this.data.location.location_state+1)%2;
        this.setData({
            [`location.location_state`]:this.data.location.location_state,
        })
    },
    onDeleteStadium:function(e){
        var that = this;
        wx.showModal({
          title:"您确定要删除场地吗？",
          success(res){
              if(res.confirm){
                //在服务器删除
                that.onShowLoading();
                wx.request({
                  url: getApp().globalData.serverUrl,
                  method:"GET",
                  timeout:10000,
                  data:{
                    //各种数据的传送
                    operation:"DELETE_PLAYGROUND",
                    stadiumId:that.data.locationId,
                    playgroundId:that.data.stadiums[e.currentTarget.dataset.index].stadium_id,
                  },
                  success(res){
                    if(res.data == false){
                        that.onHideLoading();
                        wx.showModal({
                            showCancel:false,
                            title:"请求失败,请重试",
                        });
                    }
                    else{
                        //成功获取到信息后的操作
                        that.data.stadiums.splice(e.currentTarget.dataset.index,1);
                        that.setData({
                            "stadiums":that.data.stadiums,
                        })
                        that.onHideLoading();
                    }
                  },
                  fail(res){
                      that.onHideLoading();
                    wx.showModal({
                        showCancel:false,
                        title:"请求超时,请重试",
                    });
                  },
                });
              };
          }
        })
    },
    onChangeStadiumState:function(e){
        var x = e.currentTarget.dataset.index;
        if(e.currentTarget.dataset.whe){
            var x = e.currentTarget.dataset.index;
            this.setData({
                [`stadiums[${x}].stadium_state`]:0,
            })
        }
        else {
            var x = e.currentTarget.dataset.index;
            this.setData({
                [`stadiums[${x}].stadium_state`]:1,
            })
        };
        this.onUpdateStadium(x);
    },
    onChangeStadiumPrice:function(e){
        var that = this;
        var index = e.currentTarget.dataset.index;
        wx.showModal({
          editable:true,
          title:"请输入该场地每小时的价格",
          type:Number,
          success(res){
              if(res.confirm){
                  var x = parseInt(res.content);
                  if(!isNaN(x)){
                  that.setData({
                      [`stadiums[${index}].stadium_price`]:x,
                  })
                  that.onUpdateStadium(index);
                  }
                  else if(isNaN(x)){
                    wx.showModal({
                      content:"您的输入有误，请重试",
                      showCancel:false,
                    })
                  }
              }
          }
        })
    },
    onChangeStadiumSports:function(e){
        var index = e.currentTarget.dataset.index;
        var value = e.detail.value;
        var that = this;
        if(value == 0){
            wx.showModal({
              title:"您不能选择\"所有项目\"，请重试",
            })
        }
        else 
        {
        this.setData({
            [`stadiums[${index}].stadium_sports`]:value,
        });
        }
        that.onUpdateStadium(index);
    },
    onChangeStadiumName:function(e){
        var that = this;
        wx.showModal({
          title:"请输入新场地名称",
          editable:true,
          success(res){
              if(res.confirm){
                that.setData({
                    [`stadiums[${e.currentTarget.dataset.index}].stadium_name`]:res.content,
                });
                that.onUpdateStadium(e.currentTarget.dataset.index);
              }
          }
        })
    },
    onCreateNewStadium:function(e){
        wx.navigateTo({
          url: '/pages/new-stadium-page/new-stadium-page'+"?locationId="+this.data.location.location_id,
        })
    },
    toPage_detailed_stadium:function(e){
        var index = e.currentTarget.dataset.index;
        var playgroundsArray = this.data.stadiums[index].playgrounds;
        wx.navigateTo({
          url: '/pages/detailed-stadiums-page/detailed-stadiums-page?playgrounds='+JSON.stringify(playgroundsArray)+'&index='+index,
        })
    },
    onConfirmChange:function(e){
        //传送数据并提示传输成功或者失败
        var that = this;
        var l = that.data.location;
        that.onShowLoading();
        wx.request({
          url: getApp().globalData.serverUrl,
          method:"GET",
          timeout:10000,
          data:{
            //各种数据的传送
            operation:"CHANGE_LOCATION",
            location_name:l.location_name,
            location_id:l.location_id,
            location:l.location,
            studentId:getApp().globalData.userInfo.studentId,
            location_type_room:l.location_type_room,
            location_type_free:l.location_type_free,
            location_open_time:l.location_open_time,
            location_close_time:l.location_close_time,
            location_sports:l.location_sports,
            location_state:l.location_state,
          },
          success(res){
            if(res.data == false){
                that.onHideLoading();
                wx.showModal({
                    showCancel:false,
                    title:"请求失败,请重试",
                });
            }
            else{
                //成功获取到信息后的操作
                wx.showModal({
                    showCancel:false,
                    title:"修改成功！",
                    success(res){
                        wx.navigateBack();
                    }
                });
                that.onHideLoading();
            }
          },
          fail(res){
              that.onHideLoading();
            wx.showModal({
                showCancel:false,
                title:"请求超时,请重试",
            });
          },
        });
    },
    onUpdateStadium:function(e){
        var index = e;
        var stadium = this.data.stadiums[index];
        var that = this;
        that.onShowLoading();
        wx.request({
          url: getApp().globalData.serverUrl,
          method:"GET",
          timeout:10000,
          data:{
            //各种数据的传送
            operation:"UPDATE_PLAYGROUND",
            playgroundId:stadium.stadium_id,
            name:stadium.stadium_name,
            price:stadium.stadium_price,
            sportsId:stadium.stadium_sports,
            state:stadium.stadium_state,
          },
          success(res){
            if(res.data == false){
                that.onHideLoading();
                wx.showModal({
                    showCancel:false,
                    title:"请求失败,请重试",
                });
            }
            else{
                //成功获取到信息后的操作
                console.log({
                    //各种数据的传送
                    operation:"UPDATE_PLAYGROUND",
                    playgroundId:stadium.stadium_id,
                    name:stadium.stadium_name,
                    price:stadium.stadium_price,
                    sportsId:stadium.stadium_sports,
                    state:stadium.stadium_state,
                  },);
                  that.onHideLoading();
            }
          },
          fail(res){
              that.onHideLoading();
            wx.showModal({
                showCancel:false,
                title:"请求超时,请重试",
            });
          },
        });
    },
    onShowLoading:function(e){
        var that = this;
        wx.showLoading({
          title: '加载中',
          mask:true,
          success(res){
              that.setData({
                  "showLoadingState":true,
              })
          }
        })
        setTimeout(that.onHideLoading,10200)
    },
    onHideLoading:function(e){
        var that = this;
        if(that.data.showLoadingState == true){
            wx.hideLoading();
            that.setData({ 
                "showLoadingState":false,
            })
        }
    },
})